package com.bugs.activity;

import cn.bugs.vo.Evection;
import cn.hutool.core.collection.CollectionUtil;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class GatewayInclusive {

    /**
     * 单文件上传部署逻辑
     */
    @Test
    public void addTable(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        Deployment demo1 = repositoryService.createDeployment()
                .addClasspathResource("bpmn/gateway-inclusive.bpmn") // 添加流程图
                .name("包含网关流程验证")
                .deploy();
        System.out.println("流程部署id===》"+demo1.getId());
        System.out.println("流程部署name===》"+demo1.getName());
    }


    /**
     * 启动实例
     * 这里启动2个，一个天数大于3天，一个小于3天
     */
    @Test
    public void startProcess(){
        // 获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        // 获取 runtime 服务
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        // 指定哪个模板
        String tempKey = "inclusive";

        Map<String, Object> paramMap = new HashMap<>();
        Evection evection = new Evection();
        //evection.setNum(5); // 流程1
        evection.setNum(2); // 流程2
        paramMap.put("evection",evection);

        // 启动流程实例
        runtimeService.startProcessInstanceByKey(tempKey,paramMap);
    }

    /**
     * 将指定流程进行流程提交操作
     */
    @Test
    public void completTask(){
        String tempKey = "inclusive";
        // 获取数据库的连接信息
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        List<Task> list = taskService.createTaskQuery().processDefinitionKey(tempKey).list();
        if(!CollectionUtil.isEmpty(list)){
            list.forEach(task->{
                taskService.complete(task.getId());
            });
        }

    }


    /**
     * 查询流程实例信息
     */
    @Test
    public void queryTask(){
        // 执行已部署的流程模板名称
        String tempKey = "inclusive";
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(tempKey) // 指定哪个流程图模板
                .list();
        if(!CollectionUtil.isEmpty(list)){
            list.forEach(x->{
                System.out.println("流程实例 id "+x.getProcessInstanceId());
                System.out.println("任务 id "+x.getId());
                System.out.println("任务负责人 "+x.getAssignee());
                System.out.println("任务名称 "+x.getName());
                System.out.println("===========================================");
            });
        }
    }
}
